<?php
/**
	TXT书库
**/
namespace app\admin\controller;
use app\common\controller\Admin;
use think\Db;
class Noveltxt extends Admin {

	public function _initialize() {
		parent::_initialize();
	}

	/**
	 * 内容列表
	 * @return [html] [页面内容]
	 */
	public function index() {
		$params = request()->param();
		$keyword = input('get.keyword') ?? null;
		$memberId = input('get.memberId') ?? null;
		$map = array();
		$page = [
            'total' => db('noveltxt')->count(),
            'page' => isset($params['page']) ? $params['page'] : 0,
            'limit' =>isset($params['limit']) ? $params['limit'] : 50
        ];
		if($keyword){
            $map['name'] = ["like","%$keyword%"];
        }
		if ($memberId) {
			$map['memberId'] = ["=",$memberId];
        }
		$list = db('noveltxt')->where($map)->page($page['page'],$page['limit'])->select();
		$page['total'] = db('noveltxt')->where($map)->count();
		$cp_member = $this->getCpsCpMembers(-1);
		foreach($list as $key=>$value){
			$id = $value['memberId'];
			$memberName = '--';
			if($id){
				$res = Db::connect("database.db_config_two")->name('cp_member')->where('uid','=',$id)->find();
				if($res){
					$memberName = $res['nickname'];
				}
			}
			$list[$key]['memberName'] =$memberName;
		}

		$data = array(
			'list' => $list,
			'cp_member' => $cp_member,
			'page' => $page,
			'keyword' => $keyword,
			'memberId' => $memberId,
		);
		$this->assign($data);
		return $this->fetch();
	}

	/**
	 * 查小说来源
	 */
	public function getCpsCpMembers($id = 0)
	{
		$res = Db::connect("database.db_config_two")->name('cp_member')->select();
		if($id>-1){
			foreach ($res as $k => $v) {
				if($v['uid'] == $id){
					return $v['nickname'];
				}
			}
			return $id ==0 ? '本站': '未知';
		}
		return $res;
		# code...
	}

	/**
	 * 上传txt
	 */
	public function Uploadtxt() {
		$config = config('attachment_upload');
		// 获取表单上传文件
		$file = request()->file('file');
		$info = $file->move($config['txtPath']);
		
		if ($info) {
			$return['status'] = 1;
			$return['info']   = array(
				"id"=>time(),
				"path"=>$config['txtPath']."/".$info->getSaveName(),
				"url"=>$config['txtPath']."/".$info->getSaveName(),
				"name"=>$_FILES['file']['name']
			);
		} else {
			$return['status'] = 0;
			$return['info']   = $file->getError();
		}
		echo json_encode($return);
		/*
		if ($info) {
			echo $config['rootPath']."/".$info->getSaveName();
		} else {
			echo $file->getError();
		}*/
		exit();
	}

	/**
	 * 添加
	 */
	public function add() {
		if (IS_POST) {
			$data   = $this->request->post();
			if(empty($data['url'])){
				return $this->error("文件不能为空");
			}
			$urlList = explode(',',$data['url']);
			$nameList = explode(',',$data['name']);
			foreach($urlList as $key=>$value){
				$newData = array(
					'memberId' => $data['memberId'],
					'name' => $nameList[$key],
					'url' => $urlList[$key],
					'create_time' => time()
				);
				Db::name('noveltxt') -> insert($newData);
			}
			return $this->success("添加成功！");
		}
	}
	
	/**
     * 删除
     */
	public function del() {
        $id = $this->getArrayParam('id');
        if (empty($id)) {
            return $this->error("非法操作！");
        }
        $map['id'] = array('in', $id);
		$fileList = db('noveltxt')->where(['id'=>$map['id']])->select();
		foreach($fileList as $value){
			if(file_exists($value['url'])){
				unlink($value['url']);
			}
		}
        $result = db('noveltxt')->where($map)->delete();
        if (false !== $result) {
            return $this->success("删除成功！");
        } else {
            return $this->error("删除失败！");
        }
    }
}